package cn.ecsd.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.ecsd.entity.BizCheckResult;
import cn.ecsd.entity.BizClaimVoucher;
import cn.ecsd.entity.BizClaimVoucherDetail;
import cn.ecsd.entity.SysEmployee;
import cn.ecsd.service.ClaimService;
import cn.ecsd.util.PageSupport;

public class ClaimAction extends ActionSupport {
	private ClaimService claimService;
	private BizClaimVoucher claimVoucher;
	private PageSupport page = new PageSupport();
	private Integer pageNo;
	Map<String, String> statusMap = new HashMap<String, String>();
	List<BizClaimVoucherDetail> detailList = new ArrayList<BizClaimVoucherDetail>();
	private int rowNumber;
	private BizCheckResult checkResult;

	public BizCheckResult getCheckResult() {
		return checkResult;
	}

	public void setCheckResult(BizCheckResult checkResult) {
		this.checkResult = checkResult;
	}

	public List<BizClaimVoucherDetail> getDetailList() {
		return detailList;
	}

	public void setDetailList(List<BizClaimVoucherDetail> detailList) {
		this.detailList = detailList;
	}

	public int getRowNumber() {
		return rowNumber;
	}

	public void setRowNumber(int rowNumber) {
		this.rowNumber = rowNumber;
	}

	public Map<String, String> getStatusMap() {
		return statusMap;
	}

	public void setStatusMap(Map<String, String> statusMap) {
		this.statusMap = statusMap;
	}

	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public ClaimService getClaimService() {
		return claimService;
	}

	public void setClaimService(ClaimService claimService) {
		this.claimService = claimService;
	}

	public PageSupport getPage() {
		return page;
	}

	public BizClaimVoucher getClaimVoucher() {
		return claimVoucher;
	}

	public void setClaimVoucher(BizClaimVoucher claimVoucher) {
		this.claimVoucher = claimVoucher;
	}

	public void setPage(PageSupport page) {
		this.page = page;
	}

	Map<String, Object> session = ActionContext.getContext().getSession();

	/**
	 * 查询报销单列表
	 * 
	 * @return
	 */
	public String searchClaimVoucher() {
		statusMap.put("待审批", "待审批");
		statusMap.put("待付款", "待付款");
		statusMap.put("已付款", "已付款");
		statusMap.put("已打回", "已打回");
		if (pageNo != null)
			page.setCurrentPageNo(pageNo);
		String posi = (String) (session.get("employee_position"));
		if ("员工".contentEquals((String) (session.get("employee_position")))) {
			statusMap.put("新创建", "新创建");
		}
		page = claimService.getClaimPage(page, claimVoucher);
		session.put("pageSupport", page);
		return "searchClaimVoucher";
	}

	/**
	 * 查看报销单详细信息
	 */
	public String getClaimVoucherById() {
		claimVoucher = claimService.getClaimVoucherById(claimVoucher.getId());
		return "getClaimVoucherById";
	}

	/**
	 * 进入都修改页面
	 */
	public String toUpdate() {
		claimVoucher = claimService.getClaimVoucherById(claimVoucher.getId());
		return "toUpdate";
	}

	/**
	 * 保存，提交报销单
	 */
	public String updateClaimVoucher() {
		BizClaimVoucher claimVoucher2 = claimService.getClaimVoucherById(claimVoucher.getId());
		claimVoucher2.setStatus(claimVoucher.getStatus());
		claimVoucher2.setEvent(claimVoucher.getEvent());
		claimVoucher2.setTotalAccount(claimVoucher.getTotalAccount());
		claimVoucher2.setModifyTime(new Timestamp(new Date().getTime()));
		if ("新创建".equals(claimVoucher.getStatus())) {
			// 如果是保存待处理人就是自己
			claimVoucher2.setSysEmployeeByNextDealSn((SysEmployee) (session.get("employee")));
		} else if ("已提交".equals(claimVoucher.getStatus())) {
			// 如果是提交待处理人就是自己的经历
			SysEmployee nextSn = claimService.getManagerByDeparId((SysEmployee) (session.get("employee")),
					claimVoucher.getTotalAccount());
			claimVoucher2.setSysEmployeeByNextDealSn(nextSn);
		}
		claimVoucher2.setBizClaimVoucherDetails(new HashSet(detailList));
		for (BizClaimVoucherDetail bizClaimVoucherDetail : detailList) {
			bizClaimVoucherDetail.setBizClaimVoucher(claimVoucher2);
		}
		claimService.updateClaimVoucher(claimVoucher2);
		return "updateClaimVoucher";
	}

	/**
	 * 进入报销单的审批界面
	 */
	public String toCheck() {
		claimVoucher = claimService.getClaimVoucherById(claimVoucher.getId());
		return "toCheck";
	}

	/**
	 * 审批结果
	 */
	public String checkClaimVoucher() {
		BizClaimVoucher claimVoucher2 = claimService.getClaimVoucherById(claimVoucher.getId());
		checkResult.setCheckTime(new Timestamp(new Date().getTime()));
		checkResult.setCheckEmployee((SysEmployee) (session.get("employee")));
		checkResult.setBizClaimVoucher(claimVoucher2);
		claimVoucher2.getBizCheckResults().add(checkResult);
		claimVoucher2.setModifyTime(new Timestamp(new Date().getTime()));
		if ("拒绝".equals(checkResult.getResult())) {
			// 如果处理结果是拒绝，修改待处理人为空，修改状态为已终止
			claimVoucher2.setSysEmployeeByNextDealSn(null);
			claimVoucher2.setStatus("已终止");
		} else if ("打回".equals(checkResult.getResult())) {
			// 如果处理结果是打回，修改待处理人为创建人，修改状态为已打回
			claimVoucher2.setSysEmployeeByNextDealSn(claimVoucher2.getSysEmployeeByCreateSn());
			claimVoucher2.setStatus("已打回");
		} else if ("通过".equals(checkResult.getResult())) {
			// 如果处理结果是通过，
			if ("财务".contentEquals((String) (session.get("employee_position")))) {
				// 如果审批人是财务，待处理人为空，状态修改为已付款
				claimVoucher2.setSysEmployeeByNextDealSn(null);
				claimVoucher2.setStatus("已付款");
			} else if ("总经理".contentEquals((String) (session.get("employee_position")))) {
				// 如果审批人是总经理，待处理人为财务，状态修改为已审批
				claimVoucher2.setSysEmployeeByNextDealSn(claimService.getFinance());
				claimVoucher2.setStatus("已审批");
			} else if ("部门经理".contentEquals((String) (session.get("employee_position")))) {
				// 如果审批人是部门经理，
				if (claimVoucher2.getTotalAccount() > 5000) {
					// 如果审批金额大于5000 提交 给总经理审批 状态为待审批
					SysEmployee nexEmplyee = claimService.getManagerByDeparId((SysEmployee) (session.get("employee")),
							claimVoucher2.getTotalAccount());
					claimVoucher2.setSysEmployeeByNextDealSn(nexEmplyee);
					claimVoucher2.setStatus("待审批");
				} else {
					// 如果金额小于5000 直接提交给财务审批 ，状态为已审批
					claimVoucher2.setSysEmployeeByNextDealSn(claimService.getFinance());
					claimVoucher2.setStatus("已审批");
				}
			}
		}
		claimService.updateClaimVoucher(claimVoucher2);
		return "checkClaimVoucher";
	}
	
	/**
	 * 进入新增页面
	 */
	public String toAdd(){
		return "toAdd";
	}
	/**
	 * 保存报销单
	 */
	public String saveClaimVoucher(){
		claimVoucher.setBizClaimVoucherDetails(new HashSet(detailList));
		for (BizClaimVoucherDetail bizClaimVoucherDetail : detailList) {
			bizClaimVoucherDetail.setBizClaimVoucher(claimVoucher);
		}
		claimVoucher.setCreateTime(new Timestamp(new Date().getTime()));
		claimVoucher.setSysEmployeeByCreateSn((SysEmployee)(session.get("employee")));
		claimVoucher.setModifyTime(new Timestamp(new Date().getTime()));
		if("新创建".equals(claimVoucher.getStatus())){
			claimVoucher.setSysEmployeeByNextDealSn((SysEmployee)(session.get("employee")));
		}else if("已提交".equals(claimVoucher.getStatus())){
			claimVoucher.setSysEmployeeByNextDealSn(claimService.getManagerByDeparId((SysEmployee)(session.get("employee")), 0));
		}
		claimService.addClaimVoucher(claimVoucher);
		return "saveClaimVoucher";
	}
	
	/**
	 * 删除报销单
	 */
	public String deleteClaimVoucherById(){
		BizClaimVoucher claimVoucher2 = claimService.getClaimVoucherById(claimVoucher.getId());
		claimService.deleteClaimVoucherById(claimVoucher2);
		return "deleteOK";
	}
}

